<template>
	<el-card>
		<el-form :inline="true" :model="state.queryForm" class="demo-form-inline">
			<el-form-item>
				<el-input v-model="state.queryForm.warehouseCode" placeholder="仓库代码" clearable />
			</el-form-item>
			<el-form-item>
				<el-input v-model="state.queryForm.name" placeholder="仓库名称" clearable />
			</el-form-item>
			<el-form-item>
				<el-input v-model="state.queryForm.address" placeholder="仓库地址" clearable />
			</el-form-item>
			<fast-user-select v-model="state.queryForm.masterUserId" @select="handleChildEvent" ></fast-user-select>
			<!-- <el-form-item>
				<el-select v-model="state.queryForm.masterUserId" placeholder="管理员" clearable style="width: 100%">
					<el-option v-for="user in userList" :key="user.id" :label="user.realName" :value="user.id"></el-option>
				</el-select>
			</el-form-item> -->
			<el-form-item>
				<el-input v-model="state.queryForm.comment" placeholder="备注" clearable />
			</el-form-item>
			<el-form-item>
				<el-button type="success" @click="getDataList()">查询</el-button>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
			</el-form-item>
			<el-form-item>
				<el-button type="danger" @click="deleteBatchHandle()">删除</el-button>
			</el-form-item>
		</el-form>
		<el-table v-loading="state.dataListLoading" :data="state.dataList" border style="width: 100%" @selection-change="selectionChangeHandle">
			<el-table-column type="selection" width="55" />
			<el-table-column prop="warehouseCode" label="仓库代码" width="180" />
			<el-table-column prop="name" label="仓库名称" width="180" />
			<el-table-column prop="masterUserId" label="管理员" width="120">
				<template #default="scope">
					<user-info :userId="scope.row.masterUserId">  </user-info>
				</template>
			</el-table-column>
			<el-table-column prop="address" label="仓库地址" />
			<el-table-column prop="comment" label="备注" />
			<el-table-column label="操作" fixed="right" header-align="center" align="center" width="150">
				<template #default="scope">
					<el-button size="small" type="primary" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
					<el-button size="small" type="danger" @click="deleteHandle(scope.row.id)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination
			:current-page="state.page"
			:page-sizes="state.pageSizes"
			:page-size="state.limit"
			:total="state.total"
			layout="total, sizes, prev, pager, next, jumper"
			@size-change="sizeChangeHandle"
			@current-change="currentChangeHandle"
		>
		</el-pagination>
	</el-card>
	<!-- 弹窗, 新增 / 修改 -->
	<add-or-update ref="addOrUpdateRef" @refreshDataList="getDataList"></add-or-update>
</template>
<script setup lang="ts" name="Maku-boot-WarehouseIndex">
import { useCrud } from '@/hooks'
import { reactive, ref, onMounted  } from 'vue'
import { IHooksOptions } from '@/hooks/interface'
import AddOrUpdate from './add-or-update.vue'
import { listUserApi } from '@/api/sys/user';
import FastUserSelect from '@/components/fast-user-select/src/fast-user-select.vue'

const handleChildEvent = (userIdList: number[]) => {
	console.log(userIdList)
	state.queryForm.masterUserId = userIdList
}

const userList = ref<any[]>([])

const state: IHooksOptions = reactive({
	dataListUrl: '/erp/warehouse/page',
	deleteUrl: '/erp/warehouse',
	queryForm: {
		id: null,
		name: '',
		warehouseCode: '',
		address: '',
		comment: '',
		masterUserId: null
	}
})

const addOrUpdateRef = ref()
const addOrUpdateHandle = (id?: number) => {
	addOrUpdateRef.value.init(id)
}

// 获取角色列表
const getUserList = () => {
	return listUserApi().then(res => {
		userList.value = res.data
	})
}

onMounted(() => {
  getUserList();
})

const { getDataList, selectionChangeHandle, sizeChangeHandle, currentChangeHandle, deleteHandle, deleteBatchHandle } = useCrud(state)
</script>
<style>
.el-table .warning-row {
	--el-table-tr-bg-color: var(--el-color-warning-light-9);
}

.el-table .success-row {
	--el-table-tr-bg-color: var(--el-color-success-light-9);
}
</style>
